﻿@{
    Layout = "~/Views/Shared/_PopupLayout.cshtml";
}

@model LineOfBusiness.Poco.AD_tblUserAccountPolicy


<style>
 fieldset {
    margin: 8px;
    border: 1px solid silver;
    padding: 8px;    
    border-radius: 4px;
}
legend{
    padding: 2px;        
}
</style>

<div>
    <form id="frm">
        <fieldset>
            <legend><strong>@Html.LOBHtmlHelper().Label("lblSelectUser", "Select User Group")</strong></legend>
                 <table style="width:100%">
            <tr>
                <td style="width:120px">
                    @Html.LOBHtmlHelper().Label("lblUserGroup", "User Group")
                </td>
                <td colspan="3">
                    @Html.LOBHtmlHelper().ComboBox("UserGroupID", "tblUserAccount", 0, new { style = "width:100%" })
                </td>
            </tr>
            <tr>
                <td style="width:120px">
                    
                </td>
                <td style="width:120px">
                    <input type="radio" id="UserID" name="select"  checked="checked" onchange="onchangeUserGroup('u')" />
                    @Html.LOBHtmlHelper().Label("lblUser", "User")
                    
                </td>
                <td style="width:120px">
                    <input type="radio" id="GroupID" name="select"  onchange="onchangeUserGroup('g')"/>
                    @Html.LOBHtmlHelper().Label("lblGroup", "Group")
                </td>
                <td>
                     <button type="button" class="positive" style="width:100%;height:25px" onclick="CopyPermission()">
                        @Html.LOBHtmlHelper().C("CopyPermission")
                    </button> 
                </td>
            </tr>
        </table>        
        </fieldset>
       

        <table style="width:100%">
            <tr>
                <td>
                    <fieldset style="padding-right:0px;padding-bottom:0px">
                        <legend><strong>@Html.LOBHtmlHelper().Label("lblFunctionList", "Function List")</strong></legend>
                        <table style="width:300px;">                    
                            <tr>
                                <td>
                                    @(Html.Kendo().TreeView()
                                        .Name("treeview" )
                                        .HtmlAttributes(new { @class = "demo-section" })
                                        .DataTextField("Name")
                                        .DataSource(dataSource => dataSource
                                            .Read(read => read
                                                .Action("FunctionList", "UserPermission")
                                            )
                                        )
                                        .Events(o=>o.Select("SelectFunction"))
                                    )
                                </td>
                            </tr>
                        </table>    
                    </fieldset>
                </td>
                <td style="width:200px">
                    <fieldset >
                        <legend><strong>@Html.LOBHtmlHelper().Label("lblPermission", "Permission")</strong></legend>
                        <table style="table-layout:fixed;width:100%;height:230px">                    
                            <tr>
                                <td style="vertical-align:top;height:230px">        
                                    <div id="divp"></div>
                                </td>
                            </tr>
                        </table>    
                    </fieldset>
                    <table style="table-layout:fixed;width:99%;">
                        
                            <tr>
                                <td>        
                                    
                                    <button type="button" id="btnSetFull" class="positive" style="width:100%;height:25px" onclick= "SetAll()">
                                    @Html.LOBHtmlHelper().C("SetFullPermission")
                                </button> 
                                    
                                </td>
                            </tr>

                            <tr>
                                <td>        
                                    <button type="button" id="btnRemove" class="positive" style="width:100%;height:25px" onclick= "RemoveAll()">
                                        @Html.LOBHtmlHelper().C("UnSetFullPermission")
                                    </button> 
                                    
                                    
                                </td>
                            </tr>
                    </table>
                </td>                
            </tr>
        </table>
    </form>
</div>

<div class="Line">
</div>


<div  style="float:right">
    <div class="buttons">
        @if(ViewBag.Edit!=-1)
        {
            <button id="btnSave" class="positive" onclick= "SavePopup()">
                <img src="@Html.BaseUrl()Content/images/toolbar/save.png" alt="" /> 
                @Html.LOBHtmlHelper().C("Save")
            </button> 
        }
        <button class="positive" onclick= "Close()">
            <img src="@Html.BaseUrl()Content/images/toolbar/close.png" alt="" /> 
            @Html.LOBHtmlHelper().C("Close")
        </button> 
    </div>
</div>

<style scoped>
            
            .demo-section {
                display: inline-block;
                vertical-align: top;
                text-align: left;
                width:350px;
                height:300px;
            }
        </style>  
<script type="text/javascript">
    var usercurrent = "";
    var perscurrent = new Array(0);
    var pcurrent = null;
    var IsChanged = false;
    $(document).ready(function ()
    {
        SetMultiPredicateComplexForCombo("UserGroupID", "FGroup.Equals(~0)", "false");
        SetCombobox("UserGroupID", "@LineOfBusiness.Core.Helper.CacheHelper.LoginUser.UserID");
        usercurrent = "@LineOfBusiness.Core.Helper.CacheHelper.LoginUser.UserID";

        document.getElementById('btnSetFull').disabled = true;
        document.getElementById('btnSetFull').className = "disabled";

        document.getElementById('btnRemove').disabled = true;
        document.getElementById('btnRemove').className = "disabled";
    });

    function SelectFunction(e) {
        if (IsNullOrEmty(usercurrent)) return;
        var model = this.dataItem(e.node);

        if (model.id.length == 2)
        {
            document.getElementById('btnSetFull').disabled = false;
            document.getElementById('btnSetFull').className = "positive";

            document.getElementById('btnRemove').disabled = false;
            document.getElementById('btnRemove').className = "positive";
        }
        else
        {
            document.getElementById('btnSetFull').disabled = true;
            document.getElementById('btnSetFull').className = "disabled";

            document.getElementById('btnRemove').disabled = true;
            document.getElementById('btnRemove').className = "disabled";
        }


        var pers = null;

        for (i = 0; i < perscurrent.length; i++) {
            if (perscurrent[i].f == model.id && perscurrent[i].u == usercurrent) {
                pers = perscurrent[i].p;
            }
        }

        if (pers == null) {
            pers = PostServer('@Url.Action("GetPermission","UserPermission")?user=' + usercurrent + '&function=' + model.id);
            perscurrent.push({ f: model.id, p: pers,u: usercurrent});
        }

        pcurrent = pers;

        $('#divp').empty();

        if (pers.length > 0)
        {
            
            var table = document.createElement("table");
            table.setAttribute("width", "100%");
            table.setAttribute("id", "tblp");

            for (i = 0; i < pers.length ; i++) {
                var row = table.insertRow(-1);
                row.setAttribute("height", "18px");
                row.setAttribute("id", "row" + pers[i].FType);

                var cell = row.insertCell(0);
                cell.setAttribute("width", "30px");

                if(pers[i].FIsActive)
                    cell.innerHTML = '<input id="chk' + pers[i].FType + '" type="checkbox" checked="checked"  onchange="onchangePermission(this)"/>';
                else
                    cell.innerHTML = '<input id="chk' + pers[i].FType + '" type="checkbox"  onchange="onchangePermission(this)"/>';

                var cell1 = row.insertCell(1);

                if(pers[i].FType=="1")
                    cell1.innerHTML = '@Html.LOBHtmlHelper().T("Excute")';
                else if (pers[i].FType == "2")
                    cell1.innerHTML = '@Html.LOBHtmlHelper().T("New")';
                else if (pers[i].FType == "3")
                    cell1.innerHTML = '@Html.LOBHtmlHelper().T("Edit")';
                else if (pers[i].FType == "4")
                    cell1.innerHTML = '@Html.LOBHtmlHelper().T("Delete")';
                else if (pers[i].FType == "5")
                    cell1.innerHTML = '@Html.LOBHtmlHelper().T("Post")';
                else if (pers[i].FType == "6")
                    cell1.innerHTML = '@Html.LOBHtmlHelper().T("UnPost")';
            }

            document.getElementById("divp").appendChild(table);
        }
    }
    function onchangeUserGroup(e)
    {
        if (e == "u")
        {
            SetMultiPredicateComplexForCombo("UserGroupID", "FGroup.Equals(~0)", "false");
        }
        else
        {
            SetMultiPredicateComplexForCombo("UserGroupID", "FGroup.Equals(~0)", "true");
        }
    }


    function onchange(e)
    {
        usercurrent = GetCombobox("UserGroupID");
        pcurrent = null;
        $('#divp').empty();

        var treeView = $("#treeview").data("kendoTreeView");
        treeView.select(".k-item:first");
    }

    function onchangePermission(e)
    {
        if (pcurrent == null) return;
        IsChanged = true;
        for (i = 0; i < pcurrent.length ; i++) {
            var c = $("#chk" + pcurrent[i].FType);
            if (c.length > 0)
                if (c.is(':checked'))
                    pcurrent[i].FIsActive = true;
                else 
                    pcurrent[i].FIsActive = false;
        }
    }

    function SavePopup()
    {
        if (IsChanged) {
            var m = new Array(0);
            for (i = 0; i < perscurrent.length; i++) {
                if (perscurrent[i].p.length > 0) {
                    for (j = 0; j < perscurrent[i].p.length; j++) {
                        m.push(perscurrent[i].p[j]);
                    }
                }
            }

            PostModel('@Url.Action("SavePermission","UserPermission")', m, function (e) {
                CloseWin(0, idWindow);
            });
        }
        else {
            CloseWin(0, idWindow);
        }
    }

    function Close() {
        CloseWin(0, idWindow);
    }

    function SetAll()
    {
        if (IsNullOrEmty(usercurrent)) {
            window.parent.ShowMessageErrorLevel1("FieldNotNull", "FieldNotNull", null, getLabel("lblUserGroup"));
            return;
        }

        var treeView = $("#treeview").data("kendoTreeView");
        var selectedNode = treeView.select();
        var model = treeView.dataItem(selectedNode);

        PostServer('@Url.Action("SetAllPermission","UserPermission")?UserID=' + usercurrent + '&Module=' + model.id);
        Reset();
    }

    function RemoveAll() {
        if (IsNullOrEmty(usercurrent)) {
            window.parent.ShowMessageErrorLevel1("FieldNotNull", "FieldNotNull", null, getLabel("lblUserGroup"));
            return;
        }


        var treeView = $("#treeview").data("kendoTreeView");
        var selectedNode = treeView.select();
        var model = treeView.dataItem(selectedNode);

        PostServer('@Url.Action("RemoveAllPermission","UserPermission")?UserID=' + usercurrent + '&Module=' + model.id);
        Reset();
    }

    function CopyPermission() {
        if (IsNullOrEmty(usercurrent)) {
            window.parent.ShowMessageErrorLevel1("FieldNotNull", "FieldNotNull", null, getLabel("lblUserGroup"));
            return;
        }
        var isuser = '0';
        if ($("#UserID").attr("checked") == "checked")
            isuser = '1'
        ShowWindowCallback('@Url.Action("CopyPermision","UserPermission")?UserID=' + usercurrent + '&IsUser=' + isuser, 550, 150, '@Html.LOBHtmlHelper().C("CopyPermission")', function (result) {
            Reset();
        });

    }

    
    function Reset()
    {
        usercurrent = GetCombobox("UserGroupID");
        perscurrent = new Array(0);
        IsChanged = false;
        pcurrent = null;
        $('#divp').empty();

        var treeView = $("#treeview").data("kendoTreeView");
        treeView.select(".k-item:first");
    }
</script>